// ===============================================================================
// scan: sequential scan of a large descriptor collection
// Created in September 2007 as a project for an Advanced Database Systems course
// (c) Reykjavik University, Björn Þór Jónsson, Kristleifur Daðason, Herwig Lejsek
// Contact author: Björn Þór Jónsson, Reykjavík University, bjorn@ru.is
// ===============================================================================

#ifndef CLUSTERMAPPER
#define CLUSTERMAPPER

#include <string>
#include <iostream>
#include <sys/stat.h>
#include <stdio.h>
#include <sys/types.h>
//#include <sys/mman.h>
#include <errno.h>
//#include <unistd.h>
#include <stdlib.h>

#include "FlexDescriptor.h"
#include "IndexEntry.h"
#include "ConfigMapper.h"

using namespace std;

// This class implements a scan of a single cluster
// Only a single scan can be active at each time,
// but the cluster can be scanned repeatedly in sequence

class ClusterMapper
{
public:
	ClusterMapper(ConfigMapper *cnfg,
		      IndexEntry *entry,
		      FILE *datafile);
	~ClusterMapper();

	// Ask how many descriptors there are
	int getNumDescriptors();

	// Map the cluster and prepare for reading
	void open();

	// Get the next descriptor, get NULL if there are no more
	FlexDescriptor *next();

	// Unmap the cluster and clean up for another scan
	void close();

private:
	// Bookkeeping info
	ConfigMapper *config;
	IndexEntry *cluster;

	// Info about the file mapping
	FILE *file;
	bool mapped;
	unsigned char *mapPtr;

	// Info for the scan
	size_t descByteSize;
	int scanNext;

	// Misc stuff of unknown reasons
	int clusterStartSkipDescs;
	int currentCluster;
};

#endif
